package com.jackie.tongzhuang;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.jackie.tongzhuang.Adapters.GongHuoShangAdapter;
import com.jackie.tongzhuang.Classes.BaseClass;
import com.jackie.tongzhuang.DataBase.DbClass;
import com.jackie.tongzhuang.Enums.Constants;
import com.jackie.tongzhuang.Models.FenQi;
import com.jackie.tongzhuang.Models.GongHuoShang;
import com.jackie.tongzhuang.Models.JinHuoDan;
import com.jackie.tongzhuang.Models.YinHangKa;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class InsertJinHuoDanActivity extends AppCompatActivity implements View.OnClickListener{

    private Spinner ghsspinner,lbspinner;
    private EditText jeedit,rqedit;
    private Button nextbutton;
    private TextView kyedtext;



    private DbClass dbClass;
    private List<GongHuoShang> ghss = new ArrayList<>();
    private YinHangKa yhk;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert_jinhuodan);
        dbClass = new DbClass(this);
        yhk = dbClass.getYinHangKa(1);

        initView();

        kyedtext.setText(BaseClass.getDecimalToStr(2,yhk.getKeYongEDu()));

        String rqtext = BaseClass.getDate();
        rqedit.setText(rqtext);


        ArrayAdapter<String> lbadapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, Constants.leiBie);
        lbadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        lbspinner.setAdapter(lbadapter);//将adapter 添加到spinner中


        ghss = dbClass.getGongHuoShangList();
        GongHuoShangAdapter ghsAdapter = new GongHuoShangAdapter(this,ghss);
        ghsspinner.setAdapter(ghsAdapter);



    }

    private void initView(){
        ghsspinner = findViewById(R.id.jhd_add_ghs);
        lbspinner = findViewById(R.id.jhd_add_lb);
        jeedit = findViewById(R.id.jhd_add_je);
        rqedit = findViewById(R.id.jhd_add_rq);
        nextbutton = findViewById(R.id.jhd_add_next);
        kyedtext = findViewById(R.id.jhd_add_kyed);
        nextbutton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.jhd_add_next:

                if(jeedit.getText().toString().isEmpty()){
                    Toast.makeText(this,"请输入进货金额",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(Double.valueOf(jeedit.getText().toString()) > yhk.getKeYongEDu()){
                    Toast.makeText(this,"银行卡钱不够了",Toast.LENGTH_SHORT).show();
                    return;
                }

                JinHuoDan jhd = new JinHuoDan();
                jhd.setJinHuoRiQi(rqedit.getText().toString());
                jhd.setJinHuoJinE(Double.valueOf(jeedit.getText().toString()));
                jhd.setLeiBieId(((int)lbspinner.getSelectedItemId())+1);
                jhd.setGongHuoShangId(((GongHuoShang)ghsspinner.getSelectedItem()).getId());

                Intent intent = new Intent(InsertJinHuoDanActivity.this, InsertFenQiActivity.class);
                intent.putExtra("jinhuodan",jhd);
                startActivity(intent);
                finish();
                break;
        }
    }
}